package CompleteFutureDemo;

import common.CalcTask;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * @author chao
 * @create 2022-02-06-16:54
 */
public class HandleTest {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        CompletableFuture<Double> cf = CompletableFuture.supplyAsync(() -> {
            if (false) {
                throw new RuntimeException("test");
            }
            return new CalcTask().RunWithResult();
        });
        CompletableFuture<String> cf2 = cf.handle((result, except) -> {
            if (except != null) {
                System.out.println("异常发生 -> " + except);
                except.printStackTrace();
                return "fail";
            } else {
                System.out.println("无异常");
                return "success";
            }
        });
        System.out.println("结果："+cf2.get());
    }
}
